package cn.java76.springboot.demo1.controller;

import cn.java76.springboot.demo1.entity.OAdmin;
import cn.java76.springboot.demo1.service.OAdminService;
import cn.java76.springboot.demo1.vo.AjaxPage;
import cn.java76.springboot.demo1.vo.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author oldliu
 * @since 1.0
 */
@RestController//返回json
@RequestMapping("/admin")
@Api("管理员相关操作")
@Slf4j //可以用log
public class OAdminController {

    @Autowired
    private OAdminService oAdminService;
//    @ApiOperation(value = "查找所有的管理员")
//    @GetMapping("findall")
//    public AjaxResult  findall() {
//        return AjaxResult.success(this.oAdminService.find());
//    }

    @ApiOperation("分页查找管理员")
    @GetMapping("search")
    public AjaxPage search(@ApiParam("页码") @RequestParam(name = "page",defaultValue = "1") int page, @ApiParam("用户名")@RequestParam(name = "uname",required = false) String uname, @ApiParam("手机号")@RequestParam(name = "tele",required = false)String tele){

        log.info("本次查询的条件是》name={},tele={},页号：{}",uname,tele,page);
        log.info("执行时间是"+new Date());
        return this.oAdminService.search(page,AjaxPage.PAGESIZE,uname, tele);
    }
    @ApiOperation("新增管理员")
    @PostMapping("add")
    public AjaxResult add( OAdmin admin){
        int add = this.oAdminService.add(admin);
        return AjaxResult.success("增加"+admin.getUname()+"成功");
    }
    @ApiOperation("修改管理员")
    @PostMapping("update")
    public AjaxResult update(OAdmin admin){
        int add = this.oAdminService.update(admin);
        return AjaxResult.success("修改"+admin.getUname()+"成功");
    }
    @ApiOperation("删除管理员")
    @GetMapping("del/{id}")
    public AjaxResult del(@ApiParam("要删除的管理员编号") @PathVariable  long id){
        int add = this.oAdminService.del(id);
        return AjaxResult.success("删除成功");
    }
    @ApiOperation("重置管理员密码")
    @GetMapping("resetpass/{id}")
    public AjaxResult resetPass(@ApiParam("要修改的管理员编号") @PathVariable  long id,@ApiParam("新密码")  String newPass){
        return AjaxResult.success("密码修改成功");
    }
    @ApiOperation("管理员登录")
    @PostMapping("login")
    public AjaxResult login(@ApiParam("登录名") String uname,@ApiParam("密码")  String upass){
        OAdmin admin = this.oAdminService.login(uname, upass);
        return admin!=null? AjaxResult.success("登录成功",admin):AjaxResult.error("登录失败，错误的用户名或者密码");
    }

}
